package com.jic.member.impl;

import com.alibaba.fastjson.JSONArray;
import com.jic.common.base.vo.RestResult;
import com.jic.common.redis.RedisClientTemplate;
import com.jic.enums.RedisKey;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.MememberUser;
import com.jic.member.mapper.MememberUserMapper;
import com.jic.member.request.MememberUserRequest;
import com.jic.member.service.MememberUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
@Component
public class MememberUserInfoServiceImpl implements MememberUserInfoService {
    @Resource
    private MememberUserMapper mememberUserMapper;
    @Autowired
    RedisClientTemplate redisClientTemplate;

    @Override
    public RestResult<String> updateMemberInfo(MememberUserRequest mememberUserRequest) {
        RestResult result = new RestResult();
        String userStr = redisClientTemplate.get(RedisKey.H5_LOGIN_TOKEN_.getName() + mememberUserRequest.getToken());
        if (StringUtils.isEmpty(userStr)){
            return RestResult.error("4000","登录失效");
        };
        MememberUser mMember = JSONArray.parseObject(userStr, MememberUser.class);
        Long userId =mMember.getId();
        MememberUser member = BeanUtil.copy(mememberUserRequest,MememberUser.class);
        member.setId(userId);
        long update = mememberUserMapper.update(member);
        result.setCode("0000");
        return result;
    }
}
